-- kill_attrib
-- created by zhuangsl
-- 每杀死n个敌人，增加属性

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,

    -- 击杀触发
    trigger = function(who, prop, arg)
        -- 要增加的属性
        local arr = string.explode(prop[2], "+");
        local attrib = arr[1];

        -- 对应的dungeon_attrib名称
        local dungeonAttrib = "kill_" .. attrib;

        -- 计数
        DungeonServiceM.addDungeonAttrib(dungeonAttrib, 1);

        -- 数量是否足够
        if DungeonServiceM.queryDungeonAttrib(dungeonAttrib) < prop[3] then
            return;
        end

        -- 先清空计数
        DungeonServiceM.costDungeonAttrib({[dungeonAttrib] = prop[3]});

        -- 提升属性
        local count  = tonumber(arr[2]);
        if who.type == OBJECT_TYPE_USER then
            -- 如果是玩家，奖励属性
            BonusM.doBonus({2, attrib, count}, "kill_attrib");
        elseif who.type == OBJECT_TYPE_AVALON_CANNON then
            -- 如果是阿瓦隆巨炮
            count = count + who.dbase:query(attrib, 0);

            who.dbase:set(attrib, count);
        end

        return true;
    end,

    desc = function(prop)
        local row = PropM.getRow(prop[1]);
        local desc = row.desc;

        -- 属性和数值
        local arr = string.explode(prop[2], "+");
        local attrib = arr[1];
        local count  = tonumber(arr[2]);

        local attribName = FieldsM.getFieldName(attrib)

        desc = string.gsub(desc, "{0}", attribName);
        desc = string.gsub(desc, "{1}", prop[3]);
        desc = string.gsub(desc, "{2}", count);

        return desc;
    end,

    record = function(target, prop, path)
        return false;
    end
};
